package com.pi.tools;



import java.lang.reflect.Method;

import java.net.URLDecoder;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;



import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;



public class Utils {



	private static Log log = LogFactory.getLog(Utils.class);



	// 编码解码

	public static final int CHARSET_ISO8859 = 0;

	public static final int CHARSET_UTF8 = 1;

	public static final int CHARSET_GBK = 2;

	public static final int CHARSET_GB2312 = 3;

	public static final int CHARSET_LATIN1 = 4;

	private static final String[] charset = { "ISO-8859-1", "utf-8", "GBK", "GB2312", "latin1" };

	// 日期Format

	public static final int FORMAT_YMD = 0;

	public static final int FORMAT_HMS = 1;

	public static final int FORMAT_HM = 2;

	public static final int FORMAT_YMDHMS = 3;

	public static final int FORMAT_YMDHM = 4;

	// 区域报警值

	public static final int QY_JR = 997;

	public static final int QY_JC = 998;

	public static final int QY_XS = 999;

	public static final int QY_FREE = 990;



	private static final String[] formats = { "yyyy-MM-dd", "HH:mm:ss", "HH:mm", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm" };



	public static String codeString(String src, int charsrc, int chardest) {

		String dest = src;

		if (dest != null) {

			try {

				dest = new String(dest.getBytes(charset[charsrc]), charset[chardest]);

			} catch (Exception e) {

				log.error(e);

			}

			return dest;

		}

		return src;

	}



	public static String codeString(byte[] bytes, int chardest) {

		if (bytes != null) {

			String dest = "";

			try {

				dest = new String(bytes, charset[chardest]);

			} catch (Exception e) {

				log.error(e);

			}

			return dest;

		}

		return "";



	}



	public static String decode(String src, int chardest) {

		String dest = src;

		if (dest != null) {

			try {

				dest = URLDecoder.decode(src, charset[chardest]);

			} catch (Exception e) {

				log.error(e);

			}

			return dest;

		}

		return src;

	}



	public static String dateToString(Date date, int format) {

		String dateStr = "";

		try {

			dateStr = new SimpleDateFormat(formats[format]).format(date);

		} catch (Exception e) {

			log.error(e);

		}

		return dateStr;

	}



	public static Date String2Date(String dateStr, String format) {

		Date date = null;

		try {

			date = new SimpleDateFormat(format).parse(dateStr);

		} catch (Exception e) {

			log.error(e);

		}

		return date;

	}



	/**

	 * list-bean转换为json

	 * 

	 * @param list

	 * @return

	 * @throws Exception

	 */

	@SuppressWarnings("unchecked")

	public static String listBeanJson(List list) {

		return listBeanJson(list, "");

	}



	public static String beanJson(Object obj) {

		return beanJson(obj, "");

	}



	@SuppressWarnings("unchecked")

	public static String listBeanJson(List list, String filters) {

		StringBuffer json = new StringBuffer("[");

		if (list != null && list.size() > 0) {

			for (int i = 0; i < list.size(); i++) {

				json.append(beanJson(list.get(i), filters));

				json.append(",");

			}

			json.deleteCharAt(json.length() - 1);

		}

		json.append("]");

		return json.toString();

	}



	@SuppressWarnings("unchecked")

	public static String beanJson(Object obj, String filters) {

		StringBuffer json = new StringBuffer("{");

		if (obj != null) {

			try {

				Method[] methods = obj.getClass().getMethods();

				for (Method method : methods) {

					Class[] classx = method.getParameterTypes();

					String key = method.getName().replace("get", "").toLowerCase();

					if (!method.getName().startsWith("get") || method.getName().startsWith("getClass") || filters.indexOf(key) != -1 || classx.length > 0)

						continue;

					Object value = method.invoke(obj, new Object[] {});

					if (value != null) {

						if (value.getClass().getName().equals("java.util.Date")) {

							value = Utils.dateToString((Date) value, Utils.FORMAT_YMDHMS);

						}

					} else {

						value = "";

					}

					json.append(key);

					json.append(":'");

					json.append(value);

					json.append("',");

				}

				json.deleteCharAt(json.length() - 1);

			} catch (Exception e) {

				log.error(e);

			}

		}

		json.append("}");

		return json.toString();

	}



	/**

	 * 添加多边形点数

	 * 

	 * @param points

	 * @return

	 */

	public static String points30(String points) {

		String[] pointsx = points.split(";");

		StringBuffer pointxy = new StringBuffer();

		if (pointsx.length < 16) {

			int pointCount = pointsx.length;

			int sideCount = (pointCount - 1);

			int pointAvg = (int) Math.ceil((16.0 - sideCount) / sideCount);// 边上的平均点数

			double[] lats = new double[pointCount];

			double[] lngs = new double[pointCount];

			System.out.println(pointAvg + "  " + sideCount);

			for (int i = 0; i < pointsx.length; i++) {

				String[] latlngs = pointsx[i].split(",");

				lats[i] = Double.parseDouble(latlngs[0]);

				lngs[i] = Double.parseDouble(latlngs[1]);

				if (i != 0) {

					double lat_A = lats[i - 1], lng_A = lngs[i - 1];

					double lat_B = lats[i], lng_B = lngs[i];

					double avgX = (lat_A - lat_B) / (pointAvg + 1);

					double avgY = (lng_A - lng_B) / (pointAvg + 1);

					double jX = lat_A;

					double jY = lng_A;

					for (int j = 0; j < pointAvg; j++) {

						jX -= avgX;

						jY -= avgY;

						pointxy.append(jX + "," + jY);

						pointxy.append(";");

					}



				}

				pointxy.append(pointsx[i]);

				pointxy.append(";");

			}

			pointxy.deleteCharAt(pointxy.length() - 1);

		}

		return pointxy.toString();

	}



	public static String getAlarmtypeInt(Integer alarmtype) {

		// 报警类型报警类型（1、紧急报警2、超速报警、3振动报警、4主电源断电、5出区域越界、6入区域越界、7疲劳驾驶、8地理栅栏（道路监控报警））

		StringBuffer alarmtypex = new StringBuffer();

		if (alarmtype != null) {

			byte bytex = alarmtype.byteValue();

			for (int i = 1, j = 7; i <= 8; i++, j--) {

				byte typex = GPSUtilities.getByteBit(bytex, j);

				if (typex == 1) {

					alarmtypex.append(i);

					alarmtypex.append(",");

				}

			}

		}

		alarmtypex.deleteCharAt(alarmtypex.length() - 1);

		return alarmtypex.toString();

	}

}

